package de.bazinga.rotw.input;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.player.Player;
import de.bazinga.rotw.player.PlayerActions;

public class GameInputHandler extends InputHandler {
	private PlayerActions playerActions = null;
	
	private LockedMouseLookHandler mouseLookHandler;
	
	private Player player = null;
	
	public GameInputHandler(Player player, float turnSpeed) {
		this.player = player;
		mouseLookHandler = new LockedMouseLookHandler( Game.getGame().getCamera(), turnSpeed );
        addToAttachedHandlers( mouseLookHandler );
		playerActions = new PlayerActions(player);
		
		setKeyBindings();
		setActions();
	}
	
	public void setKeyBindings() {
		KeyBindingManager.getKeyBindingManager().set("moveUp",KeyInput.KEY_UP);
		KeyBindingManager.getKeyBindingManager().set("moveDown",KeyInput.KEY_DOWN);
		KeyBindingManager.getKeyBindingManager().set("moveLeft",KeyInput.KEY_LEFT);
		KeyBindingManager.getKeyBindingManager().set("moveRight",KeyInput.KEY_RIGHT);
		
		

		// With shift
		KeyBindingManager.getKeyBindingManager().add("sneak",KeyInput.KEY_LSHIFT);
		KeyBindingManager.getKeyBindingManager().add("duck",KeyInput.KEY_LMENU);
		KeyBindingManager.getKeyBindingManager().add("duck", KeyInput.KEY_C);

		KeyBindingManager.getKeyBindingManager().add("nightvision",KeyInput.KEY_N);
		
		// WASD
		KeyBindingManager.getKeyBindingManager().add("moveUp", KeyInput.KEY_W);
		KeyBindingManager.getKeyBindingManager().add("moveDown",KeyInput.KEY_S);
		KeyBindingManager.getKeyBindingManager().add("moveLeft",KeyInput.KEY_A);
		KeyBindingManager.getKeyBindingManager().add("moveRight",KeyInput.KEY_D);
		


		//KeyBindingManager.getKeyBindingManager().set("jump",KeyInput.KEY_SPACE); REPLACE BY SPACE
		
		KeyBindingManager.getKeyBindingManager().set("gotoMenu",KeyInput.KEY_ESCAPE);
		
		KeyBindingManager.getKeyBindingManager().set("statistic",KeyInput.KEY_TAB);
		
		
		
		/**
		 * Chat
		 */
		KeyBindingManager.getKeyBindingManager().set("talk",KeyInput.KEY_T);
		KeyBindingManager.getKeyBindingManager().set("send",KeyInput.KEY_RETURN);
		KeyBindingManager.getKeyBindingManager().set("del",KeyInput.KEY_BACK);
		

		KeyBindingManager.getKeyBindingManager().set(".",KeyInput.KEY_PERIOD);
		KeyBindingManager.getKeyBindingManager().set(",",KeyInput.KEY_COMMA);
		KeyBindingManager.getKeyBindingManager().set("?",KeyInput.KEY_MINUS);
		KeyBindingManager.getKeyBindingManager().add("SPACE",KeyInput.KEY_SPACE);
		
		
		KeyBindingManager.getKeyBindingManager().set("1",KeyInput.KEY_1);
		KeyBindingManager.getKeyBindingManager().set("2",KeyInput.KEY_2);
		KeyBindingManager.getKeyBindingManager().set("3",KeyInput.KEY_3);
		KeyBindingManager.getKeyBindingManager().set("4",KeyInput.KEY_4);
		KeyBindingManager.getKeyBindingManager().set("5",KeyInput.KEY_5);
		KeyBindingManager.getKeyBindingManager().set("6",KeyInput.KEY_6);
		KeyBindingManager.getKeyBindingManager().set("7",KeyInput.KEY_7);
		KeyBindingManager.getKeyBindingManager().set("8",KeyInput.KEY_8);
		KeyBindingManager.getKeyBindingManager().set("9",KeyInput.KEY_9);
		KeyBindingManager.getKeyBindingManager().set("0",KeyInput.KEY_0);
		

		KeyBindingManager.getKeyBindingManager().set("A", KeyInput.KEY_A);
		KeyBindingManager.getKeyBindingManager().set("B", KeyInput.KEY_B);
	    KeyBindingManager.getKeyBindingManager().set("C", KeyInput.KEY_C);
        KeyBindingManager.getKeyBindingManager().set("D", KeyInput.KEY_D);
        KeyBindingManager.getKeyBindingManager().set("E", KeyInput.KEY_E);
        KeyBindingManager.getKeyBindingManager().set("F", KeyInput.KEY_F);
        KeyBindingManager.getKeyBindingManager().set("G", KeyInput.KEY_G);
        KeyBindingManager.getKeyBindingManager().set("H", KeyInput.KEY_H);
        KeyBindingManager.getKeyBindingManager().set("I", KeyInput.KEY_I);
        KeyBindingManager.getKeyBindingManager().set("J", KeyInput.KEY_J);
        KeyBindingManager.getKeyBindingManager().set("K", KeyInput.KEY_K);
        KeyBindingManager.getKeyBindingManager().set("L", KeyInput.KEY_L);
	    KeyBindingManager.getKeyBindingManager().set("M", KeyInput.KEY_M);
	    KeyBindingManager.getKeyBindingManager().set("N", KeyInput.KEY_N);
	    KeyBindingManager.getKeyBindingManager().set("O", KeyInput.KEY_O);
	    KeyBindingManager.getKeyBindingManager().set("P", KeyInput.KEY_P);
	    KeyBindingManager.getKeyBindingManager().set("Q", KeyInput.KEY_Q);
	    KeyBindingManager.getKeyBindingManager().set("R", KeyInput.KEY_R);
	    KeyBindingManager.getKeyBindingManager().set("S", KeyInput.KEY_S);
	    KeyBindingManager.getKeyBindingManager().add("T", KeyInput.KEY_T);
	    KeyBindingManager.getKeyBindingManager().set("U", KeyInput.KEY_U);
	    KeyBindingManager.getKeyBindingManager().set("V", KeyInput.KEY_V);
	    KeyBindingManager.getKeyBindingManager().set("W", KeyInput.KEY_W);
	    KeyBindingManager.getKeyBindingManager().set("X", KeyInput.KEY_X);
	    KeyBindingManager.getKeyBindingManager().set("Y", KeyInput.KEY_Y);
	    KeyBindingManager.getKeyBindingManager().set("Z", KeyInput.KEY_Z);
	}
	
	public void setActions() {
		
		this.addAction(playerActions, "moveUp", true);
		this.addAction(playerActions, "moveDown", true);
		this.addAction(playerActions, "moveLeft", true);
		this.addAction(playerActions, "moveRight", true);

		this.addAction(playerActions, "jump", false);
		this.addAction(playerActions, "nightvision", false);

		GameActions gameActions = new GameActions();
		
		this.addAction(gameActions, "gotoMenu", false);
		
		this.addAction(gameActions, "statistic", true);
		
		
		
		/**
		 * Chat
		 */
		this.addAction(playerActions, "talk", false);
		this.addAction(playerActions, "send", false);
		this.addAction(playerActions, "del", false);
		
		String[] chatInput = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".",",","?","SPACE"};
		
		for(String s:chatInput) {
			this.addAction(playerActions, s, false);
		}
	}
	
	public void update(float tpf) {
		super.update(tpf);
		
		
		if(player != null) {
			playerActions.update(tpf);
		}
	}
}